package com.cn.yugan.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cn.yugan.Engine.Http.AddReqCookie;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.Utils.StringConv;
import com.cn.yugan.Utils.ToastUtils;
import com.cn.yugan.mobile.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class ReCommentActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editText;
    private Button button;
    private String commment;

    private String aid;
    private int floor = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recomment);
        init();

    }


    private void init() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("我的评论");
        actionBar.setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        aid = intent.getStringExtra("aid");
        floor = intent.getIntExtra("floor", 0);

        if (floor > 0) {
            actionBar.setTitle("回复#" + floor + "楼");
        }
        editText = (EditText) findViewById(R.id.recomment_et);
        button = (Button) findViewById(R.id.recomment_bt);
        button.setOnClickListener(this);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }

   

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.recomment_bt) {
            commment = editText.getText().toString();
            String url = getString(R.string.serverurl) + getString(R.string.addcomment);
            AsyncHttpClient asyncHttpClient = new AsyncHttpClient(true,80,443);
            RequestParams requestParams = new RequestParams();
            requestParams.put("aid", aid);
            requestParams.put("reply", floor);
            requestParams.put("content", commment);
            LogUtils.d(AddReqCookie.getNormalheader(this));
            asyncHttpClient.addHeader("cookie", AddReqCookie.getNormalheader(this));
            asyncHttpClient.post(this, url, requestParams, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    try {
                        JSONObject jsonObject = new JSONObject(new String(bytes, "UTF-8"));
                        if (jsonObject.getBoolean("success")) {
                            ToastUtils.show(ReCommentActivity.this, "回复成功");
                            CommentActivity.instance.refreshdata();
                            ReCommentActivity.this.finish();
                        } else {
                            ToastUtils.show(ReCommentActivity.this, "回复失败");
                            LogUtils.d(StringConv.Unicode2UTF8(new String(bytes, "UTF-8")));

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    ToastUtils.show(ReCommentActivity.this, "网络传输失败");
                }
            });
           
        }
    }
}
